123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653 |
- <template>
- <div>
- <div class="w-full h-[55px] sm:h-[72px]"></div>
- <ErrorBoundary :error="error">
- <div v-if="isLoading" class="flex justify-center py-12">
- <!-- 加载中 -->
- <div
- class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"
- ></div>
- </div>
-
- <div v-else>
- <!-- 面包屑导航 -->
- <div class="max-w-full mb-6 xl:px-2 lg:px-2 md:px-4 px-4 mt-6">
- <div class="max-w-screen-2xl mx-auto">
- <nuxt-link
- to="/"
- class="justify-start text-white/60 text-base font-normal"
- >ホーム</nuxt-link
- >
- <span class="text-white/60 text-base font-normal px-2"> / </span>
- <nuxt-link
- to="/products"
- class="text-white/60 text-base font-normal"
- >製品一覧</nuxt-link
- >
- <span class="text-white/60 text-base font-normal px-2"> / </span>
- <nuxt-link
- v-if="product?.category"
- :to="`/products?category=${encodeURIComponent(product.category)}`"
- class="text-white/60 text-base font-normal"
- >{{ product.category }}</nuxt-link
- >
- <span class="text-white/60 text-base font-normal px-2"> / </span>
- <span class="text-white text-base font-normal">{{
- product?.title || product?.name
- }}</span>
- </div>
- </div>
-
- <!-- 产品详情内容 -->
- <div
- v-if="product"
- class="max-w-full mb-12 md:mb-20 lg:mb-32 xl:px-2 lg:px-2 md:px-4 px-4"
- >
- <div class="max-w-screen-2xl mx-auto">
- <div class="grid grid-cols-1 lg:grid-cols-2 gap-8 lg:gap-16">
- <!-- 左侧产品图片 -->
- <div class="flex flex-col gap-6">
- <!-- 主图展示 -->
- <div
- class="bg-zinc-900 rounded-lg p-8 relative overflow-hidden group aspect-square"
- >
- <!-- 加载状态 -->
- <div
- v-if="isImageLoading"
- class="absolute inset-0 flex items-center justify-center bg-zinc-800/50 z-10"
- >
- <div
- class="animate-spin h-8 w-8 border-4 border-blue-500 rounded-full border-t-transparent"
- ></div>
- </div>
-
- <!-- 主图容器 -->
- <div class="relative w-full h-full">
- <!-- 当前图片 -->
- <img
- :src="currentImage"
- :alt="product.name"
- class="absolute inset-0 w-full h-full object-contain rounded-lg transition-all duration-500"
- :class="{
- 'opacity-0': isImageLoading,
- 'opacity-100': !isImageLoading,
- }"
- @load="handleImageLoad"
- @error="handleImageError"
- />
-
- <!-- 预加载图片 -->
- <img
- v-if="preloadImage"
- :src="preloadImage"
- class="absolute inset-0 w-full h-full object-contain rounded-lg opacity-0"
- @load="handlePreloadComplete"
- />
- </div>
-
- <!-- 错误提示 -->
- <div
- v-if="imageError"
- class="absolute inset-0 flex items-center justify-center bg-red-900/50 z-20"
- >
- <div class="flex flex-col items-center gap-2">
- <span class="text-white"
- >画像の読み込みに失敗しました</span
- >
- <button
- @click.stop="retryLoadImage"
- class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors duration-300"
- >
- 再試行
- </button>
- </div>
- </div>
- </div>
-
- <!-- 缩略图列表 -->
- <div class="flex gap-4 overflow-x-auto pb-2 scrollbar-hide">
- <div
- v-for="(image, index) in [
- product.image,
- ...(product.gallery || []),
- ]"
- :key="index"
- @click="changeImage(image)"
- class="flex-shrink-0 w-20 h-20 cursor-pointer rounded-lg transition-all duration-300 relative group aspect-square p-0.5"
- :class="{
- 'bg-gradient-to-r from-blue-500 to-blue-600':
- currentImage === image,
- 'hover:bg-gradient-to-r hover:from-blue-500/50 hover:to-blue-600/50':
- currentImage !== image,
- 'opacity-50':
- isThumbnailLoading[index] || thumbnailErrors[index],
- }"
- >
- <!-- 缩略图加载状态 -->
- <div
- v-if="isThumbnailLoading[index]"
- class="absolute inset-0 flex items-center justify-center bg-zinc-800 rounded-lg"
- >
- <div
- class="animate-spin h-4 w-4 border-2 border-blue-500 rounded-full border-t-transparent"
- ></div>
- </div>
-
- <!-- 缩略图遮罩 -->
- <div
- class="absolute inset-0 bg-black/0 transition-all duration-300 rounded-lg"
- :class="{
- 'bg-black/30': currentImage === image,
- 'group-hover:bg-black/20': currentImage !== image,
- }"
- ></div>
-
- <img
- :src="image"
- :alt="`${product.name} - 画像 ${index + 1}`"
- class="w-full h-full object-cover transition-all duration-300 rounded-lg"
- :class="{
- 'opacity-0': isThumbnailLoading[index],
- 'opacity-100': !isThumbnailLoading[index],
- 'group-hover:scale-110': currentImage !== image,
- }"
- @load="handleThumbnailLoad(index)"
- @error="handleThumbnailError(index)"
- />
-
- <!-- 选中标记 -->
- <div
- v-if="currentImage === image"
- class="absolute top-1 right-1 w-4 h-4 bg-blue-500 rounded-full flex items-center justify-center"
- >
- <div class="w-2 h-2 bg-white rounded-full"></div>
- </div>
-
- <!-- 缩略图错误提示 -->
- <div
- v-if="thumbnailErrors[index]"
- class="absolute inset-0 flex items-center justify-center bg-red-900/50 rounded-lg"
- >
- <div class="flex flex-col items-center gap-1">
- <span class="text-white text-xs">エラー</span>
- <button
- @click.stop="retryLoadThumbnail(index)"
- class="px-2 py-1 bg-blue-500 text-white text-xs rounded hover:bg-blue-600 transition-colors duration-300"
- >
- 再試行
- </button>
- </div>
- </div>
- </div>
- </div>
- </div>
-
- <!-- 右侧产品信息 -->
- <div class="flex flex-col gap-8">
- <!-- 产品名称 -->
- <div class="bg-zinc-900 rounded-lg p-6">
- <h1 class="text-white text-3xl font-medium mb-4">
- {{ product.title || product.name }}
- </h1>
- <div class="text-stone-400 text-lg leading-relaxed">
- {{ product.summary }}
- </div>
- </div>
-
- <!-- 产品参数 -->
- <div class="bg-zinc-900 rounded-lg p-6">
- <h2 class="text-white text-xl font-medium mb-6">製品仕様</h2>
- <div class="grid grid-cols-1 gap-4">
- <div
- class="flex justify-between items-center py-2 border-b border-zinc-800"
- >
- <span class="text-stone-400">カテゴリー</span>
- <span class="text-white font-medium">{{
- product.category
- }}</span>
- </div>
- <div
- class="flex justify-between items-center py-2 border-b border-zinc-800"
- >
- <span class="text-stone-400">用途</span>
- <span class="text-white font-medium">{{
- product.usage?.join(", ")
- }}</span>
- </div>
- <div class="flex justify-between items-center py-2">
- <span class="text-stone-400">容量</span>
- <span class="text-white font-medium">{{
- product.capacities?.join(" / ")
- }}</span>
- </div>
- </div>
- </div>
-
- <!-- 产品描述 -->
- <div class="bg-zinc-900 rounded-lg p-6">
- <h2 class="text-white text-xl font-medium mb-6">产品描述</h2>
- <div
- class="text-stone-400 leading-relaxed space-y-4 prose prose-invert max-w-none"
- >
- {{ product.description }}
- </div>
- </div>
-
- <div class="bg-zinc-900 rounded-lg p-6">
- <h2 class="text-white text-xl font-medium mb-6">详细描述</h2>
- <div
- class="text-stone-400 leading-relaxed space-y-4 prose prose-invert max-w-none"
- >
- <ContentRenderer :value="product.content" />
- </div>
- </div>
-
- <!-- 相关产品 -->
- <div
- v-if="relatedProducts.length > 0"
- class="bg-zinc-900 rounded-lg p-6"
- >
- <h2 class="text-white text-xl font-medium mb-6">
- {{
- product.meta?.series && product.meta.series.length > 0
- ? "同シリーズ製品"
- : "関連製品"
- }}
- </h2>
- <div
- class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4"
- >
- <nuxt-link
- v-for="relatedProduct in relatedProducts"
- :key="relatedProduct.id"
- :to="`/products/${relatedProduct.id}`"
- class="group"
- >
- <div
- class="bg-zinc-800 rounded-lg p-4 transition-all duration-300 hover:bg-zinc-700"
- >
- <div
- class="aspect-square mb-4 overflow-hidden rounded-lg"
- >
- <img
- :src="relatedProduct.image"
- :alt="relatedProduct.title || relatedProduct.name"
- class="w-full h-full object-cover transition-transform duration-300 group-hover:scale-110"
- />
- </div>
- <h3
- class="text-white text-lg font-medium mb-2 line-clamp-2"
- >
- {{ relatedProduct.title || relatedProduct.name }}
- </h3>
- <p class="text-stone-400 text-sm line-clamp-2">
- {{ relatedProduct.summary }}
- </p>
- </div>
- </nuxt-link>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </ErrorBoundary>
- </div>
- </template>
-
- <script setup lang="ts">
- /**
- * 产品详情页面
- * 展示产品主图、参数和描述
- */
- import { useErrorHandler } from "~/composables/useErrorHandler";
- import { useRoute, useI18n, useAsyncData } from "#imports";
- import { queryCollection } from "#imports";
- import { ContentRenderer } from "#components";
-
- const { error, isLoading } = useErrorHandler();
- const route = useRoute();
- const { locale, t } = useI18n();
- const id = route.params.id as string;
-
- // 图片状态
- const currentImage = ref<string>("");
- const isImageLoading = ref(true);
- const isThumbnailLoading = ref<boolean[]>([]);
- const imageError = ref(false);
- const thumbnailErrors = ref<boolean[]>([]);
- const preloadImage = ref<string | null>(null);
-
- interface Product {
- id: string;
- name: string;
- usage: string[];
- capacities: string[];
- category: string;
- description: string;
- summary: string;
- image: string;
- gallery: string[];
- body: string;
- content?: any;
- meta?: {
- series?: string[];
- name?: string;
- title?: string;
- image?: string;
- summary?: string;
- };
- title?: string;
- }
-
- /**
- * 使用queryCollection获取产品数据
- */
- const { data: productContent } = await useAsyncData(`product-${id}`, async () => {
- try {
- // 使用queryCollection从content目录获取数据
- const content = await queryCollection("content")
- .where("path", "LIKE", `/products/${locale.value}/${id}`)
- .first();
- return content;
- } catch (err) {
- console.error("Error fetching product content:", err);
- error.value = new Error(t("products.loadError"));
- return null;
- }
- });
-
- /**
- * 获取分类信息
- */
- const { data: categoryContent } = await useAsyncData(
- `category-${productContent.value?.categoryId}`,
- async () => {
- if (!productContent.value?.categoryId) return null;
- try {
- const content = await queryCollection("content")
- .where("path", "LIKE", `/categories/${locale.value}/${productContent.value.categoryId}`)
- .first();
- return content;
- } catch (err) {
- console.error("Error fetching category:", err);
- return null;
- }
- },
- {
- immediate: !!productContent.value?.categoryId
- }
- );
-
- /**
- * 使用计算属性解析产品数据
- */
- const product = computed<Product | null>(() => {
- if (!productContent.value) return null;
-
- // 提取产品数据
- const meta = productContent.value.meta || {};
-
- return {
- id: id,
- name: String(meta.name || productContent.value.title || ""),
- title: String(productContent.value.title || meta.name || ""),
- usage: Array.isArray(meta.usage) ? meta.usage : [],
- capacities: Array.isArray(meta.capacities) ? meta.capacities : [],
- category: categoryContent.value?.title || "",
- description: productContent.value.description || "",
- summary: String(meta.summary || ""),
- image: String(meta.image || ""),
- gallery: Array.isArray(meta.gallery) ? meta.gallery : [],
- body: productContent.value.body || "",
- content: productContent.value,
- meta: {
- series: Array.isArray(meta.series) ? meta.series : [],
- name: String(meta.name || ""),
- title: String(productContent.value.title || ""),
- image: String(meta.image || ""),
- summary: String(meta.summary || ""),
- },
- };
- });
-
- /**
- * 获取相关产品
- */
- const { data: relatedProductsContent } = await useAsyncData(
- `related-products-${id}`,
- async () => {
- try {
- // 获取产品列表
- const content = await queryCollection("content")
- .where("path", "LIKE", `/products/${locale.value}/%`)
- .all();
- return content;
- } catch (err) {
- console.error("Error fetching related products:", err);
- return [];
- }
- }
- );
-
- /**
- * 处理相关产品数据
- */
- const relatedProducts = computed(() => {
- if (!relatedProductsContent.value || !product.value) return [];
-
- return relatedProductsContent.value
- .filter((item: any) => item._path !== `/products/${locale.value}/${id}`)
- .map((item: any) => {
- const meta = item.meta || {};
- return {
- id: item._path?.split('/').pop() || "",
- name: meta.name || item.title || "",
- title: item.title || meta.name || "",
- image: meta.image || "",
- summary: meta.summary || "",
- };
- })
- .slice(0, 6); // 最多显示6个相关产品
- });
-
- /**
- * 预加载下一张图片
- */
- function preloadNextImage(image: string) {
- preloadImage.value = image;
- }
-
- /**
- * 处理预加载完成
- */
- function handlePreloadComplete() {
- preloadImage.value = null;
- }
-
- /**
- * 处理图片加载完成
- */
- function handleImageLoad() {
- isImageLoading.value = false;
- imageError.value = false;
- }
-
- /**
- * 处理图片加载错误
- */
- function handleImageError() {
- isImageLoading.value = false;
- imageError.value = true;
- }
-
- /**
- * 重试加载图片
- */
- function retryLoadImage() {
- isImageLoading.value = true;
- imageError.value = false;
- // 强制重新加载图片
- const img = new Image();
- img.src = currentImage.value;
- img.onload = () => {
- handleImageLoad();
- };
- img.onerror = () => {
- handleImageError();
- };
- }
-
- /**
- * 重试加载缩略图
- */
- function retryLoadThumbnail(index: number) {
- isThumbnailLoading.value[index] = true;
- thumbnailErrors.value[index] = false;
- // 强制重新加载缩略图
- const img = new Image();
- const images = [product.value?.image, ...(product.value?.gallery || [])];
- img.src = images[index] || "";
- img.onload = () => {
- handleThumbnailLoad(index);
- };
- img.onerror = () => {
- handleThumbnailError(index);
- };
- }
-
- /**
- * 处理缩略图加载完成
- */
- function handleThumbnailLoad(index: number) {
- isThumbnailLoading.value[index] = false;
- thumbnailErrors.value[index] = false;
- }
-
- /**
- * 处理缩略图加载错误
- */
- function handleThumbnailError(index: number) {
- isThumbnailLoading.value[index] = false;
- thumbnailErrors.value[index] = true;
- }
-
- /**
- * 切换图片
- */
- function changeImage(image: string | undefined) {
- if (image && image !== currentImage.value) {
- isImageLoading.value = true;
- imageError.value = false;
- preloadNextImage(image);
- currentImage.value = image;
- }
- }
-
- // 页面加载时初始化状态
- onMounted(() => {
- // 设置当前图片
- if (product.value?.image) {
- currentImage.value = product.value.image;
- }
-
- // 初始化缩略图加载状态数组
- const galleryLength = (product.value?.gallery?.length || 0) + 1; // +1是因为主图也算一张
- isThumbnailLoading.value = Array(galleryLength).fill(true);
- thumbnailErrors.value = Array(galleryLength).fill(false);
- });
-
- // SEO优化
- useHead(() => ({
- title: `${product.value?.name || "产品详情"} - Hanye`,
- meta: [
- {
- name: "description",
- content: product.value?.description || "产品详情页面",
- },
- ],
- }));
- </script>
-
- <style scoped>
- /* 隐藏滚动条但保持滚动功能 */
- .scrollbar-hide {
- -ms-overflow-style: none; /* IE and Edge */
- scrollbar-width: none; /* Firefox */
- }
- .scrollbar-hide::-webkit-scrollbar {
- display: none; /* Chrome, Safari and Opera */
- }
-
- /* 图片过渡动画 */
- .main-image {
- transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
- }
-
- /* 缩略图悬停效果 */
- .thumbnail-item {
- transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
- }
-
- .thumbnail-item:hover {
- transform: translateY(-2px);
- }
-
- /* 缩略图选中效果 */
- .thumbnail-item.selected {
- transform: scale(1.05);
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
- 0 2px 4px -1px rgba(0, 0, 0, 0.06);
- }
-
- /* 产品信息卡片效果 */
- .info-card {
- transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
- }
-
- .info-card:hover {
- transform: translateY(-2px);
- box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
- 0 2px 4px -1px rgba(0, 0, 0, 0.06);
- }
-
- /* 添加 prose 样式 */
- .prose {
- @apply text-stone-400;
- }
-
- .prose h1,
- .prose h2,
- .prose h3,
- .prose h4,
- .prose h5,
- .prose h6 {
- @apply text-white font-medium;
- }
-
- .prose a {
- @apply text-blue-400 hover:text-blue-300;
- }
-
- .prose ul,
- .prose ol {
- @apply list-disc list-inside;
- }
-
- .prose blockquote {
- @apply border-l-4 border-zinc-700 pl-4 italic;
- }
-
- .prose code {
- @apply bg-zinc-800 px-1 py-0.5 rounded;
- }
-
- .prose pre {
- @apply bg-zinc-800 p-4 rounded-lg overflow-x-auto;
- }
-
- .prose img {
- @apply rounded-lg;
- }
- </style>
|